package com.yanqu.road.server.logic.bag;

import com.yanqu.road.entity.bag.UseGoodsResult;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.patrons.PatronsModule;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.server.manger.log.LogMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 增加门客实力
 */
public class UseAddPatronsAbility extends BaseUseGoods {

	public UseAddPatronsAbility(GamePlayer player, GoodsInfo goodsInfo, long param) {
		super(player, goodsInfo, param);
	}

	@Override
	int checkUse(long count) {
		int occupation = goodsInfo.getParamList().get(1).intValue();
		if(occupation >= 0){
			UserPatrons userPatrons = player.getModule(PatronsModule.class).getRandomPatrons(occupation);
			if(null == userPatrons){
				return GameErrorCode.E_BAG_ITEM_USE_NO_OCCUPATION_PATRONS;
			}
		}
		return 0;
	}

	@Override
	UseGoodsResult executeUse(long count) {
		UseGoodsResult result = new UseGoodsResult();
		long isRandomAbility = goodsInfo.getParamList().get(0).longValue();
		int occupation = goodsInfo.getParamList().get(1).intValue();
		int patronsId = (int) param;
		UserPatrons userPatrons = null;
		PatronsModule patronsModule = player.getModule(PatronsModule.class);
		if(-1 == occupation){
			if(0 == param){
				result.setRet(GameErrorCode.E_BAG_ITEM_USE_NO_SET_PATRONS);
				return result;
			}
			userPatrons = patronsModule.getUserPatrons(patronsId);
			if(null == userPatrons){
				result.setRet(GameErrorCode.E_PATRONS_NO_FOUND);
				return result;
			}
		}
		Property property = new Property();
		List<UserPatrons> syncList = new ArrayList<>();

		// 标记门客被加了多少次
		Map<Integer, Integer> patronsAddNumMap = new HashMap<>();

		for(int i = 0; i < count; i++) {
			long addAbility = 0;
			if(0 == isRandomAbility){
				addAbility = GoodsMgr.getAddRandomValue(goodsInfo).longValue();
			}else{
				addAbility = isRandomAbility;
			}
			UserPatrons randomPatrons = userPatrons;
			if(null == randomPatrons) {
				randomPatrons = patronsModule.getRandomPatrons(occupation);
			}
			if(null != randomPatrons) {

				property.addProperty(randomPatrons.getPatronsId(), BigInteger.valueOf(addAbility));
				if(!syncList.contains(randomPatrons)){
					syncList.add(randomPatrons);
				}

				if (!patronsAddNumMap.containsKey(randomPatrons.getPatronsId())) {
					patronsAddNumMap.put(randomPatrons.getPatronsId(), 0);
				}
				patronsAddNumMap.put(randomPatrons.getPatronsId(), patronsAddNumMap.get(randomPatrons.getPatronsId()) + 1);
			}
		}

		// 统一日志及逻辑处理 防止因道具一次使用太多导致逻辑冗余处理及日志冗余
		for (Map.Entry<Integer, BigInteger> entry : property.getGoods().entrySet()) {
			int addPatronsId = entry.getKey();
			long addValue = entry.getValue().longValue();
			UserPatrons addUserPatrons = patronsModule.getUserPatrons(addPatronsId);

			if (addUserPatrons != null) {
				BigInteger oldValue = addUserPatrons.getAddAbility();
				addUserPatrons.setAddAbility(addUserPatrons.getAddAbility().add(BigInteger.valueOf(addValue)));
				LogMgr.addLogUseAddAttributeGoods(player.getUserId(), goodsInfo.getGoodsId(), goodsInfo.getType(), addPatronsId, addValue,
						oldValue, addUserPatrons.getAddAbility(), patronsAddNumMap.getOrDefault(addPatronsId, 0));
			}

		}

		if(syncList.size() > 0){
			patronsModule.onPatronsListChange(syncList);
			patronsModule.syncPatronsData(syncList, false);
		}
		result.setReward(PropertyHelper.parsePropertyToString(property));
		return result;
	}
}
